<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="roleForm" :rules="rules" label-width="220px">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="roleForm.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号">
                <el-input v-model="roleForm.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input v-model="roleForm.mobile" :disabled="!!$route.params.id" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <selectTree v-model="roleForm.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="roleForm.formOfEmployment" size="mini" class="inputW" placeholder="请选择聘用方式">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="roleForm.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="roleForm.correctionTime"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <image-upload v-model="roleForm.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="submitUpdate">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import { getEmployeeDetail, updateEmployee, addEmployee } from '@/api/employee'
import selectTree from './components/select-tree.vue'
import imageUpload from './components/image-upload.vue'
export default {
  components: { selectTree, imageUpload },
  data() {
    return {
      roleForm: {
        username: '',
        workNumber: '',
        mobile: '',
        departmentId: null,
        formOfEmployment: '',
        timeOfEntry: null,
        correctionTime: null,
        id: null,
        staffPhoto: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' },
          {
            trigger: 'blur',
            // 注意，此处必须写成方法，validator: () => {}
            validator: (rule, value, callback) => {
              // 转正时间不能小于入职时间
              if (new Date(value) < new Date(this.roleForm.timeOfEntry)) {
                callback(new Error('转正时间不能小于入职时间'))
              } else {
                callback()
              }
            }
          }
        ]
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      if (!this.$route.params.id) return
      const res = await getEmployeeDetail(this.$route.params.id)
      // console.log(res)
      this.roleForm = res
    },
    // 提交修改
    async submitUpdate() {
      await this.$refs.userForm.validate()
      // console.log('校验通过')
      if (this.$route.params.id) {
        // 有查询参数则为修改，提交修改请求
        await updateEmployee(this.roleForm)
        // 提示用户
        this.$message.success('修改员工成功')
      } else {
        // 没有则为新增，提交新增请求
        await addEmployee(this.roleForm)
        // 提示用户
        this.$message.success('新增员工成功')
      }
      // 清空表单及数据
      this.roleForm = {
        username: '',
        workNumber: '',
        mobile: '',
        departmentId: null,
        formOfEmployment: '',
        timeOfEntry: '',
        correctionTime: '',
        id: null,
        staffPhoto: ''
      }
      // 跳转到员工列表页面
      this.$router.push('/employee')
    }
  }
}
</script>

<style scoped lang="scss">
.edit-form {
  background: #fff;
  padding: 20px;

  .inputW {
    width: 380px
  }

  ::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  ::v-deep(.avatar-uploader .el-upload:hover) {
    border-color: #409EFF;
  }

  ::v-deep(.avatar-uploader-icon) {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  ::v-deep(.avatar) {
    width: 178px;
    height: 178px;
    display: block;
  }

}
</style>
